// pages/finance/offline/offline.js
import { debounce } from '../../../utils/Tools'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: '',
    values:'',
    customerList:[],
    customer_id:'',
    id:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  onChange:debounce(function(e){

    if(e.detail!==''){
      wx.request({
        url:'http://master.gddzxcx.com/'+'mobileadmin/opening_number_add',
        data:{
          token:wx.getStorageSync('token'),
          customer_name:e.detail
        },
        method:"POST",
        success:res=>{
          if(res.data.code===200){
            this.setData({
              customerList:res.data.data,
              values:''
            })
          }
        },
        fail:err=>{
          console.log(err)
        }
      })
    }else{
      this.setData({
      values:''
      })
    }
    
  },500),
  onChanges(e){
    console.log(e)
  },
  chooseIndex(e){
    console.log(e)
    let customerList=this.data.customerList;
    let index=e.currentTarget.dataset.index;
    this.setData({
      value:customerList[index].customer_name,
      values:customerList[index].opening_bill.opening_amount_total,
      customer_id:customerList[index].opening_bill.customer_id,
      customerList:[],
      id:customerList[index].opening_bill.id,
    })
  },
  save(){
    let value=this.data.value;
    let values=this.data.values;
    if(value===''&&values===""){
      wx.showToast({
        title: '请填写完整的数据，在提交',
        icon: 'none',
        duration: 1500,
        mask: true
        });
    }else{
      wx.request({
        url:'http://master.gddzxcx.com/'+'mobileadmin/opening_number_add_do',
        data:{
          token:wx.getStorageSync('token'),
          customer_id:this.data.customer_id+'',
          get_money:this.data.values,
          opening_bill_id:this.data.id
        },
        method:'POST',
        success:res=>{
          console.log(res)
        },
        fail:err=>{
          console.log(err)
        }
      })
    }
  },

  
})